var mainApp = angular.module("mainApp", [ "ui.router", "ui.bootstrap",
		"httpPost", "ui.select", "ngFileUpload" ]);

mainApp.controller("mainController", function($rootScope, $scope, $http,
		$location, $sce) {
	var am = $scope.am = {};
	$http.post("index.do").success(function(data) {
		am.menu = data.resourceList;
		am.currentUser = data.currentUser;
	});

	var clientHeight = document.documentElement.clientHeight;
	$(".container-custom").css("height", clientHeight - 52);

	$scope.$on("repeatFinish", function(repeatFinish) {
		$(".sub-menu>a").click(function() {
			$(this).parent().siblings().removeClass("active");
			$(this).parent().addClass("active");
		});

		$(".sub>li>a").click(function() {
			$(".sub>li").removeClass("active");
			$(this).parent().addClass("active");
		});

		var active = function(path) {
			var lis = $("ul.sub>li");
			var p = path;
			angular.forEach(lis, function(li) {
				var li = angular.element(li);
				var href = li.attr("ui-sref");
				if (p == "/" + href) {
					li.hasClass("active") && li.removeClass("active");
					li.parent().parent().hasClass("active")
							&& li.parent().parent().removeClass("active");
					li.addClass("active");
					li.parent().parent().addClass("active");
				}
			});
		};

		active($location.path());
	})

	$rootScope.alert = function(message) {
		$rootScope.alertMessage = $sce.trustAsHtml(message);
		$("#alert").modal("show");
	}

	$rootScope.confirm = function(message, fn) {
		$rootScope.confirmMessage = $sce.trustAsHtml(message);
		$("#confirm").modal("show");
		$("#makesure").one("click", function() {
			fn(), $("#confirm").modal("hide");
		});
	}

});

mainApp.config(function($stateProvider, $urlRouterProvider) {
	var um = {};
	$.ajax({
		url : "index.do",
		async : false,
		success : function(data) {
			um = data.resourceList;
		}
	});
	function setRoute(res) {
		if (res.url != null && res.url != "" && res.url.indexOf(".") < 0) {
			var config = {
				url : "/" + res.url,
				templateUrl : "../" + res.url + ".jsp"
			};
			$stateProvider.state(res.url, config);
			return $stateProvider;
		}
	}
	angular.forEach(um[0].resourceList, function(res) {
		if (angular.isDefined(res.resourceList)) {
			angular.forEach(res.resourceList, function(sonRes) {
				setRoute(sonRes);
			})
		}
		setRoute(res);
	});
	setRoute({
		url : 'admin/default'
	});
	$urlRouterProvider.otherwise("admin/default");

});

mainApp.directive('repeatFinish', function($timeout) {
	return {
		restrict : "A",
		link : function(scope, element) {
			if (scope.$last == true) {
				$timeout(function() {
					scope.$emit('repeatFinish');
				})
			}
		}
	}
})

mainApp.directive("ngPath", function($timeout) {
	return {
		scope : {},
		restrict : "A",
		template : "<ol class='breadcrumb'><li>{{menu}}</li>"
				+ "<li class='active'>{{sonMenu}}</li></ol>",
		link : function(scope, el) {
			$timeout(function() {
				scope.menu = $(".sub-menu.active>a").text();
				scope.sonMenu = $(".sub>li.active>a").text();
			}, 100);
		}
	}
})

mainApp.filter("limitLength", function() {
	return function(str, length) {
		if (str && str.length > length) {
			str = str.substring(0, length) + "...";
		}
		return str;
	}
})
